﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.VisionDemo
{
    public partial class Frm摇筛装置 : Form
    {
        Plc siemens;
        private System.Threading.Timer _timer;

        public Frm摇筛装置()
        {
            InitializeComponent();
            // 创建计时器，但不立即启动
            _timer = new System.Threading.Timer(TimerCallback, null, Timeout.Infinite, Timeout.Infinite);
        }

        private void TimerCallback(object state)
        {
            // 这里模拟获取数据（在后台线程）
            //string timeString = System.DateTime.Now.ToString("HH:mm:ss.fff");
            //txt_摇摆伺服速度.Text = timeString;


            //读取摇摆伺服使能状态
            bool 摇摆伺服使能状态 = (bool)siemens.Read(DataType.DataBlock, 1, 4300, VarType.Bit, 1);
            //Console.WriteLine($"读取值: {摇摆伺服使能状态}");
            //读取纵向伺服使能状态
            bool 纵向伺服使能状态 = (bool)siemens.Read(DataType.DataBlock, 1, 3300, VarType.Bit, 1);
            //Console.WriteLine($"读取值: {纵向伺服使能状态}");
            //读取横向伺服使能状态
            bool 横向伺服使能状态 = (bool)siemens.Read(DataType.DataBlock, 1, 2300, VarType.Bit, 1);
            //Console.WriteLine($"读取值: {横向伺服使能状态}");



            //读取摇摆伺服实际位置
            int 摇摆伺服实际位置 = (int)siemens.Read(DataType.DataBlock, 1, 4026, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {摇摆伺服实际位置}");
            //读取纵向伺服实际位置
            int 纵向伺服实际位置 = (int)siemens.Read(DataType.DataBlock, 1, 3026, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {纵向伺服实际位置}");
            //读取横向伺服实际位置
            int 横向伺服实际位置 = (int)siemens.Read(DataType.DataBlock, 1, 2026, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {横向伺服实际位置}");


            //读取摇摆伺服状态
            ushort 摇摆伺服状态 = (ushort)siemens.Read(DataType.DataBlock, 1, 4032, VarType.Word, 1, 0);
            //Console.WriteLine($"读取值: {摇摆伺服状态}");
            //读取纵向伺服状态
            ushort 纵向伺服状态 = (ushort)siemens.Read(DataType.DataBlock, 1, 3032, VarType.Word, 1, 0);
            //Console.WriteLine($"读取值: {纵向伺服状态}");
            //读取横向伺服状态
            ushort 横向伺服状态 = (ushort)siemens.Read(DataType.DataBlock, 1, 2032, VarType.Word, 1, 0);
            //Console.WriteLine($"读取值: {横向伺服状态}");

            //读取电爪开口实际值
            byte 电爪开口实际值  = (byte)siemens.Read(DataType.DataBlock, 1, 12000, VarType.Byte, 1);
            //读取电动夹爪初始化状态
            bool 电动夹爪初始化状态 = (bool)siemens.Read(DataType.DataBlock, 1, 9000, VarType.Bit, 1);

            // 使用BeginInvoke将更新操作封送到UI线程
            BeginInvoke((Action)(() =>
            {


                #region 使能状态
                if (摇摆伺服使能状态)
                {
                    if (btn_摇摆伺服使能.BackColor != Color.Red)
                        btn_摇摆伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_摇摆伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_摇摆伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (横向伺服使能状态)
                {
                    if (btn_横向伺服使能.BackColor != Color.Red)
                        btn_横向伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_横向伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_横向伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (纵向伺服使能状态)
                {
                    if (btn_纵向伺服使能.BackColor != Color.Red)
                        btn_纵向伺服使能.BackColor = Color.Red;
                }
                else
                {
                    if (btn_纵向伺服使能.BackColor != Color.FromArgb(0, 0, 64))
                        btn_纵向伺服使能.BackColor = Color.FromArgb(0, 0, 64);
                }

                if (电动夹爪初始化状态)
                {
                    if (lbl_电动夹爪初始化状态.ForeColor != Color.Red)
                        lbl_电动夹爪初始化状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_电动夹爪初始化状态.ForeColor != Color.FromArgb(0, 0, 64))
                        lbl_电动夹爪初始化状态.ForeColor = Color.FromArgb(0, 0, 64);
                }

                #endregion

                #region 实际位置
                if (txt_横向伺服实际位置.Text != 横向伺服实际位置.ToString())
                    txt_横向伺服实际位置.Text = 横向伺服实际位置.ToString();

                if (txt_纵向伺服实际位置.Text != 纵向伺服实际位置.ToString())
                    txt_纵向伺服实际位置.Text = 纵向伺服实际位置.ToString();

                if (txt_摇摆伺服实际位置.Text != 摇摆伺服实际位置.ToString())
                    txt_摇摆伺服实际位置.Text = 摇摆伺服实际位置.ToString();

                #endregion

                #region 伺服状态
                if (横向伺服状态 != (ushort)0)
                {
                    if (lbl_横向伺服状态.ForeColor != Color.Red)
                        lbl_横向伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_横向伺服状态.ForeColor != Color.Blue)
                        lbl_横向伺服状态.ForeColor = Color.Blue;
                }

                if (纵向伺服状态 != (ushort)0)
                {
                    if (lbl_纵向伺服状态.ForeColor != Color.Red)
                        lbl_纵向伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_纵向伺服状态.ForeColor != Color.Blue)
                        lbl_纵向伺服状态.ForeColor = Color.Blue;
                }

                if (摇摆伺服状态 != (ushort)0)
                {
                    if (lbl_摇摆伺服状态.ForeColor != Color.Red)
                        lbl_摇摆伺服状态.ForeColor = Color.Red;
                }
                else
                {
                    if (lbl_摇摆伺服状态.ForeColor != Color.Blue)
                        lbl_摇摆伺服状态.ForeColor = Color.Blue;
                }


                #endregion

                #region 电爪开口值

                if (txt_电动夹爪开口实际值.Text != 电爪开口实际值.ToString())
                    txt_电动夹爪开口实际值.Text = 电爪开口实际值.ToString();

                #endregion

            }));
        }


        private void btnPlcConnect_Click(object sender, EventArgs e)
        {
            try
            {


                siemens = new Plc(CpuType.S71200, txtPlcIP.Text, 102, 0, 1);


                // 连接对象
                siemens.Open();
                if (!siemens.IsConnected)
                {
                    labStatus.Text = "连接PLC失败！";
                }
                else
                {
                    labStatus.Text = "连接PLC成功！";
                    readInitialValue();
                }
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void readInitialValue()
        {

            //读取摇摆伺服模式设定
            ushort 摇摆伺服模式设定 = (ushort)siemens.Read(DataType.DataBlock, 1, 4000, VarType.Word, 1);
            //Console.WriteLine($"读取值: {摇摆伺服模式设定}");
            //读取纵向伺服模式设定
            ushort 纵向伺服模式设定 = (ushort)siemens.Read(DataType.DataBlock, 1, 3000, VarType.Word, 1);
            //Console.WriteLine($"读取值: {纵向伺服模式设定}");
            //读取横向伺服模式设定
            ushort 横向伺服模式设定 = (ushort)siemens.Read(DataType.DataBlock, 1, 2000, VarType.Word, 1);
            //Console.WriteLine($"读取值: {横向伺服模式设定}");


            if (txt_摇摆伺服模式设定.Text != 摇摆伺服模式设定.ToString())
                txt_摇摆伺服模式设定.Text = 摇摆伺服模式设定.ToString();

            if (txt_纵向伺服模式设定.Text != 纵向伺服模式设定.ToString())
                txt_纵向伺服模式设定.Text = 纵向伺服模式设定.ToString();

            if (txt_横向伺服模式设定.Text != 横向伺服模式设定.ToString())
                txt_横向伺服模式设定.Text = 横向伺服模式设定.ToString();


            //读取摇摆伺服速度设定
            int 摇摆伺服速度设定 = (int)siemens.Read(DataType.DataBlock, 1, 4012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {摇摆伺服速度设定}");
            txt_摇摆伺服速度设定.Text = 摇摆伺服速度设定.ToString();

            //读取纵向伺服速度设定
            int 纵向伺服速度设定 = (int)siemens.Read(DataType.DataBlock, 1, 3012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {纵向伺服速度设定}");
            txt_纵向伺服速度设定.Text = 纵向伺服速度设定.ToString();

            //读取横向伺服速度设定
            int 横向伺服速度设定 = (int)siemens.Read(DataType.DataBlock, 1, 2012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {横向伺服速度设定}");
            txt_横向伺服速度设定.Text = 横向伺服速度设定.ToString();



            //读取摇摆伺服周期设定
            int 摇摆伺服周期设定 = (int)siemens.Read(DataType.DataBlock, 1, 206, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {摇摆伺服周期设定}");
            txt_摇摆伺服周期设定.Text = 摇摆伺服周期设定.ToString();

            //读取摇摆伺服目标位置
            int 摇摆伺服目标位置 = (int)siemens.Read(DataType.DataBlock, 1, 4008, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {摇摆伺服目标位置}");
            txt_摇摆伺服目标位置.Text = 摇摆伺服目标位置.ToString();

            //读取纵向伺服目标位置
            int 纵向伺服目标位置 = (int)siemens.Read(DataType.DataBlock, 1, 3008, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {纵向伺服目标位置}");
            txt_纵向伺服目标位置.Text = 纵向伺服目标位置.ToString();

            //读取横向伺服目标位置
            int 横向伺服目标位置 = (int)siemens.Read(DataType.DataBlock, 1, 2008, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {横向伺服目标位置}");
            txt_横向伺服目标位置.Text = 横向伺服目标位置.ToString();

            //读取电爪开口设定值
            txt_电动夹爪开口设置值.Text = siemens.Read(DataType.DataBlock, 1, 12000, VarType.Byte, 1).ToString();
        }


        private void btnPlcDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                siemens.Close();
                labStatus.Text = "PLC断开连接！";
                //updateTimer.Enabled = false;
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void txt_摇摆伺服模式_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt_摇摆伺服位置_TextChanged(object sender, EventArgs e)
        {
            ushort newValue = ushort.Parse(txt_摇摆伺服周期设定.Text);
            byte[] bytes = new byte[] {
            (byte)(newValue >> 8),
            (byte)(newValue & 0xFF),
            };
            siemens.Write(DataType.DataBlock, 1, 4008, bytes);

        }

        private void btn_摇摆伺服使能_Click(object sender, EventArgs e)
        {

            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 4300, VarType.Bit, 1, 0);
            Console.WriteLine($"读取值: {valueRead}");
            if ((bool)siemens.Read(DataType.DataBlock, 1, 4300, VarType.Bit, 1, 0))
            {
                siemens.WriteBit(DataType.DataBlock, 1, 4300, 0, false);

            }
            else 
            { 
            siemens.WriteBit(DataType.DataBlock, 1, 4300, 0, true);
            
            }

        }


        private void btn_摇摆伺服模式设定_Click(object sender, EventArgs e)
        {
            //ushort newValue = 1;
            //byte[] bytes = new byte[] {
            //(byte)(newValue >> 8),
            //(byte)(newValue & 0xFF),
            //};

            //siemens.WriteBytes(DataType.DataBlock, 1, 4000, bytes);

            ushort newValue = ushort.Parse(txt_摇摆伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 1, 4000, newValue);

        }

        private void btn_摇摆伺服周期设定_Click(object sender, EventArgs e)
        {
            //ushort newValue = ushort.Parse(txt_摇摆伺服周期设置.Text);
            //byte[] bytes = new byte[] {
            //(byte)(newValue >> 8),
            //(byte)(newValue & 0xFF),
            //};
            int newValue = int.Parse(txt_摇摆伺服周期设定.Text);
            siemens.Write(DataType.DataBlock, 1, 206, newValue);
        }

        private void btn_横向伺服模式设定_Click(object sender, EventArgs e)
        {
            //ushort newValue = ushort.Parse(txt_横向伺服模式.Text);
            //byte[] bytes = new byte[] {
            //(byte)(newValue >> 8),
            //(byte)(newValue & 0xFF),
            //};

            //siemens.WriteBytes(DataType.DataBlock, 1, 2000, bytes);
            ushort newValue = ushort.Parse(txt_横向伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 1, 2000, newValue);
        }

        private void btn_横向伺服目标位置_Click(object sender, EventArgs e)
        {
            //ushort newValue = ushort.Parse(txt_横向伺服位置.Text);
            //byte[] bytes = new byte[] {
            //(byte)(newValue >> 8),
            //(byte)(newValue & 0xFF),
            //};
            //siemens.Write(DataType.DataBlock, 1, 2008, bytes);
            int newValue = int.Parse(txt_横向伺服目标位置.Text);
            siemens.Write(DataType.DataBlock, 1, 2008, newValue);
        }

        private void btn_横向伺服使能_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read(DataType.DataBlock, 1, 2300, VarType.Bit, 1, 0))
            {
                siemens.WriteBit(DataType.DataBlock, 1, 2300, 0, false);

            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 1, 2300, 0, true);

            }
        }



        private void btn_纵向伺服模式设定_Click(object sender, EventArgs e)
        {
            //ushort newValue = ushort.Parse(txt_纵向伺服模式.Text);
            //byte[] bytes = new byte[] {
            //(byte)(newValue >> 8),
            //(byte)(newValue & 0xFF),
            //};

            //siemens.WriteBytes(DataType.DataBlock, 1, 3000, bytes);
            ushort newValue = ushort.Parse(txt_纵向伺服模式设定.Text);
            siemens.Write(DataType.DataBlock, 1, 3000, newValue);
        }

        private void btn_纵向伺服目标位置_Click(object sender, EventArgs e)
        {
            //ushort newValue = ushort.Parse(txt_纵向伺服位置.Text);
            //byte[] bytes = new byte[] {
            //(byte)(newValue >> 8),
            //(byte)(newValue & 0xFF),
            //};
            //siemens.Write(DataType.DataBlock, 1, 3008, bytes);
            int newValue = int.Parse(txt_纵向伺服目标位置.Text);
            siemens.Write(DataType.DataBlock, 1, 3008, newValue);

        }

        private void btn_纵向伺服使能_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read(DataType.DataBlock, 1, 3300, VarType.Bit, 1, 0))
            {
                siemens.WriteBit(DataType.DataBlock, 1, 3300, 0, false);

            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 1, 3300, 0, true);

            }
        }




        private void btn_摇摆伺服后退_Click(object sender, EventArgs e)
        {

        }

        private void btn_摇摆伺服前进_Click(object sender, EventArgs e)
        {

        }

        private void btn_摇摆伺服向前_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 4040, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 4040, 2, true);
        }

        private void btn_摇摆伺服向前_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 4040, VarType.Bit, 1, 2);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 4040, 2, false);
        }

        private void btn_摇摆伺服向后_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 4040, VarType.Bit, 1, 3);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 4040, 3, true);
        }

        private void btn_摇摆伺服向后_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 4040, VarType.Bit, 1, 3);
            //Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 4040, 3, false);
        }

        private void btn_纵向伺服上_MouseDown(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 4, true);

        }

        private void btn_纵向伺服上_MouseUp(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 4, false);

        }

        private void btn_横向伺服后退_MouseDown(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 3, true);

        }

        private void btn_横向伺服后退_MouseUp(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 3, false);

        }

        private void btn_横向伺服前进_MouseDown(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 2, true);

        }

        private void btn_横向伺服前进_MouseUp(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 2, false);

        }

        private void btn_摇摆伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 4300, VarType.Bit, 1, 3);
            Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 4300, 3, true);

        }

        private void btn_摇摆伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 4300, VarType.Bit, 1, 3);
            Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 4300, 3, false);
        }

        private void btn_电动夹爪开口设置_Click(object sender, EventArgs e)
        {
            //ushort newValue = ushort.Parse(txt_电动夹爪开口设置值.Text);
            //byte[] bytes = new byte[] {
            //(byte)(newValue >> 8),
            //(byte)(newValue & 0xFF),
            //};
            //siemens.Write(DataType.DataBlock, 1, 12000, bytes);


            if (!byte.TryParse(txt_电动夹爪开口设置值.Text, out byte byteValue))
            {
                MessageBox.Show("请输入有效的字节值", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            siemens.Write(DataType.DataBlock, 1, 12000, byteValue);

        }

        private void btn_电动夹爪开口值加_Click(object sender, EventArgs e)
        {
            txt_电动夹爪开口设置值.Text = (int.Parse(txt_电动夹爪开口设置值.Text) + 1).ToString();
            btn_电动夹爪开口设置值.PerformClick();
        }

        private void btn_电动夹爪开口值减_Click(object sender, EventArgs e)
        {
            txt_电动夹爪开口设置值.Text = (int.Parse(txt_电动夹爪开口设置值.Text) - 1).ToString();
            btn_电动夹爪开口设置值.PerformClick();
        }

        private void btn_电动夹爪开口实际值_Click(object sender, EventArgs e)
        {
            txt_电动夹爪开口实际值.Text = siemens.Read(DataType.DataBlock, 1, 12000, VarType.Byte, 1).ToString();

        }

        private void btn_纵向伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 3300, VarType.Bit, 1, 3);
            Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 3300, 3, true);
        }

        private void btn_纵向伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 3300, VarType.Bit, 1, 3);
            Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 3300, 3, false);
        }

        private void btn_横向伺服启动_MouseDown(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 2300, VarType.Bit, 1, 3);
            Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 2300, 3, true);
        }

        private void btn_横向伺服启动_MouseUp(object sender, MouseEventArgs e)
        {
            bool valueRead = (bool)siemens.Read(DataType.DataBlock, 1, 2300, VarType.Bit, 1, 3);
            Console.WriteLine($"读取值: {valueRead}");
            siemens.WriteBit(DataType.DataBlock, 1, 2300, 3, false);
        }

        private void btn_纵向伺服下_MouseDown(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 5, true);

        }

        private void btn_纵向伺服下_MouseUp(object sender, MouseEventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 5, false);

        }

        private void btn_震动器启动_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 301, 2, true);
            //siemens.WriteBit(DataType.DataBlock, 1, 301, 4, false);
            siemens.WriteBit(DataType.DataBlock, 1, 301, 2, false);

        }

        private void btn_震动器停止_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 301, 4, true);
            //siemens.WriteBit(DataType.DataBlock, 1, 301, 2, false);
            siemens.WriteBit(DataType.DataBlock, 1, 301, 4, false);



        }

        private void btn_气缸上行_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 301, 5, true);

            siemens.WriteBit(DataType.DataBlock, 1, 301, 5, false);
        }

        private void btn_气缸下行_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 301, 6, true);

            siemens.WriteBit(DataType.DataBlock, 1, 301, 6, false);
        }

        private void btn_气缸停止_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 302, 1, true);
    
            siemens.WriteBit(DataType.DataBlock, 1, 302, 1, false);
        }

        private void btn_StartTimer_Click(object sender, EventArgs e)
        {
            btn_StartTimer.BackColor = Color.DodgerBlue;
            btn_StopTimer.BackColor = Color.FromArgb(0, 0, 64);
            // 启动计时器，每秒更新一次（1000毫秒）
            if (siemens != null && siemens.IsConnected)
            {
                _timer.Change(0, 1000);

            }
            else
            {

            }

        }

        private void btn_StopTimer_Click(object sender, EventArgs e)
        {
            btn_StartTimer.BackColor = Color.FromArgb(0, 0, 64);
            btn_StopTimer.BackColor = Color.DodgerBlue;
            // 停止计时器
            _timer.Change(Timeout.Infinite, Timeout.Infinite);

        }

        private void btn_摇摆伺服速度设定_Click(object sender, EventArgs e)
        {
            int valueRead = (int)siemens.Read(DataType.DataBlock, 1, 4012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            int newValue = int.Parse(txt_摇摆伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 1, 4012, newValue);

            //读取摇摆伺服速度设定
            int 摇摆伺服速度设定 = (int)siemens.Read(DataType.DataBlock, 1, 4012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {摇摆伺服速度设定}");
            txt_摇摆伺服速度设定.Text = 摇摆伺服速度设定.ToString();
        }

        private void btn_纵向伺服速度设定_Click(object sender, EventArgs e)
        {
            int valueRead = (int)siemens.Read(DataType.DataBlock, 1, 3012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            int newValue = int.Parse(txt_纵向伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 1, 3012, newValue);

            //读取纵向伺服速度设定
            int 纵向伺服速度设定 = (int)siemens.Read(DataType.DataBlock, 1, 3012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {纵向伺服速度设定}");
            txt_纵向伺服速度设定.Text = 纵向伺服速度设定.ToString();
        }

        private void btn_横向伺服速度设定_Click(object sender, EventArgs e)
        {
            int valueRead = (int)siemens.Read(DataType.DataBlock, 1, 2012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {valueRead}");

            int newValue = int.Parse(txt_横向伺服速度设定.Text);

            siemens.Write(DataType.DataBlock, 1, 2012, newValue);

            //读取横向伺服速度设定
            int 横向伺服速度设定 = (int)siemens.Read(DataType.DataBlock, 1, 2012, VarType.DInt, 1);
            //Console.WriteLine($"读取值: {横向伺服速度设定}");
            txt_横向伺服速度设定.Text = 横向伺服速度设定.ToString();
        }
        private void tb_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmSoftKey.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmSoftKey frmSoftKey = new FrmSoftKey();
            // 计算窗体的位置
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y + tb_temp.Size.Height;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmSoftKey.confirm)
            {
                tb_temp.Text = FrmSoftKey.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
            }
        }

        private void btn_摇摆伺服目标位置_Click(object sender, EventArgs e)
        {
            int newValue = int.Parse(txt_摇摆伺服目标位置.Text);
            siemens.Write(DataType.DataBlock, 1, 4008, newValue);
        }

        private void btn_电动夹爪初始化_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 9100, 2, true);

        }

        private void 顶平1A_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 303, 0, true);

            siemens.WriteBit(DataType.DataBlock, 1, 303, 0, false);
        }

        private void 顶平2A_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 303, 2, true);

            siemens.WriteBit(DataType.DataBlock, 1, 303, 2, false);
        }

        private void 顶平1B_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 303, 1, true);

            siemens.WriteBit(DataType.DataBlock, 1, 303, 1, false);
        }

        private void 顶平2B_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 303, 3, true);

            siemens.WriteBit(DataType.DataBlock, 1, 303, 3, false);
        }

        private void 顶平1停_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 303, 4, true);

            siemens.WriteBit(DataType.DataBlock, 1, 303, 4, false);
        }

        private void 顶平2停_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 303, 5, true);

            siemens.WriteBit(DataType.DataBlock, 1, 303, 5, false);
        }
    }
}
